package com.macro.mall.common.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * @author: TuDou
 * @create: 2024-09-04 17:24
 *
 * 序列化金额
 *用法
 *@JsonSerialize(using = CustomerDoubleSerialize.class)
 *
 **/
public class CustomerDoubleSerialize extends JsonSerializer {
    private DecimalFormat df = new DecimalFormat("0.00");

    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(o.toString() != null && !"-".equals(o)) {
            Double dd=Double.parseDouble(o.toString());

            jsonGenerator.writeString(df.format(dd));

        } else{
            jsonGenerator.writeString(o.toString());

        }
    }
}